<?php
/*
 * 解释器模式
 * 解释类经常执行非常相似的任务，所以最好仔细检查创建的类是否重复
 */
function my_autoloader($class) {
    //var_dump($class);
    include $class . '.php';
}
spl_autoload_register('my_autoloader');

//
$context = new InterpreterContext();
$literal = new LiteralExpression('four');
$literal->interpret($context);
print $context->lookup($literal)."\n<BR/>";



//
$context = new InterpreterContext();
$myvar = new VariableExpression('input','four');
$myvar->interpret($context);
print $context->lookup($myvar)."\n<BR/>";


$newvar = new VariableExpression('input');
$newvar->interpret($context);
print $context->lookup($newvar)."\n<BR/>";

$myvar->setValue('five');
$myvar->interpret($context);
print $context->lookup($myvar)."\n<BR/>";
print $context->lookup($newvar)."\n<BR/>";

//
$context = new InterpreterContext();
$input = new VariableExpression('input');
$statement = new BooleanOrExpression(
	new EqualsExpression($input,new LiteralExpression('four')),
	new EqualsExpression($input,new LiteralExpression(4))
);

foreach(array("four","4","52") as $val){
	$input->setValue($val);
	print "$val:\n<BR/>";
	$statement ->interpret($context);
	if($context->lookup($statement)){
		print "top marks \n\n<BR/><BR/>";
	}else{
		print "dunce hat on \n\n<BR/><BR/>";
	}
}